var studentQueryUrl = basePath + "/sys/student/groupup/json/studentList";
var batchCreatePdfUrl = basePath + "/sys/student/groupup/json/batchCreate";
var createPdfByStudentIdUrl = basePath + "/sys/student/groupup/json/createByStudentId";
var getFileListUrl = basePath + "/sys/student/groupup/json/getFileList";

var dataGrid;
$(function() {
	initGradeList();
	loadData();
});

function loadData() {
	dataGrid = $('#studentDg').datagrid({
		url : studentQueryUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		rownumbers : true,
		queryParams: {
			grade: $("#grade").val()
		},
		columns : [ [  {
			field : 'id',
			width : 20,
			align : "center",
			checkbox : true
		}, {
			field : 'action',
			title : '操作',
			resizable : true,
			align : "center",
			width : 20,
			formatter : function(value,row,index){
				if(row.isGroupup == 1) {
					var str = $.formatString('<button  type="button" class="btn btn-success btn-xs" style="margin:4px 4px;" onclick="getFileList(\'{0}\');">查看</button>', row.id);
					str += $.formatString('<button  type="button" class="btn btn-danger btn-xs" style="margin:4px 4px;" onclick="createPdfByStudentId(\'{0}\');">重新生成</button>', row.id);
					return str
				}
				return $.formatString('<button  type="button" class="btn btn-danger btn-xs" style="margin:4px 4px;" onclick="createPdfByStudentId(\'{0}\');">生成</button>', row.id);;
			}
		}, {
			field : 'name',
			title : '学生姓名',
			resizable : true,
			align : "center",
			width : 20
		}, {
			field : 'idNo',
			title : '身份证号',
			width : 30,
			align : "center"
		}, {
			field : 'grade',
			title : '年级',
			width : 30,
			align : "center",
			formatter: function(value) {
				return value + "级";
			}
		}, {
			field : 'clazz',
			title : '班级',
			width : 30,
			align : "center"
		}] ]
		
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function initGradeList() {
	$.ajax({
 		url : basePath + "/getFullGrade",
 		type : "post",
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			var html = '<option value=""></option>';
 			for(var i in data){
 		        if (data.hasOwnProperty(i)) {
 		        	html += "<option value='"+data[i]+"' "+(i==0?"selected='selected'":"")+">"+ data[i] +"级</option>";
 		        };
 		    }
 			$("#grade").html(html);
 			$("#grade").trigger("onchange");
 		}, error:function(){
 			alert("查询年级异常");
  		}
    });
}

function searchClazz(grade) {
	if(!grade) return;
	$.ajax({
 		url : basePath + "/getFullClazz?grade=" + grade,
 		type : "GET",
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			var html = '<option value=""></option>';
 			for(var i in data){
 		        if (data.hasOwnProperty(i)) {
 		        	html += "<option value='"+data[i]+"'>"+ data[i] +"班</option>";
 		        };
 		    }
 			$("#clazz").html(html);
 		}, error:function(){
 			alert("查询班级异常");
  		}
    });
}

function batchCreatePdf(formId) {
	$.messager.confirm('系统提示', '是否确认批量生成成长足迹', function(r) {
		if (r){
			$.ajax({
				url : batchCreatePdfUrl,
				type : 'POST',
				error : function() {
					$.messager.progress('close');
					$.messager.alert('系统提示', '操作异常', 'error');
				},
				data : $('#' + formId).serialize(),
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						$.messager.alert('系统提示', '批量提交生成,请过几分钟再查看结果', 'info');
					} else {
						$.messager.alert('系统提示', '批量提交失败', 'warning');
					}
				}
			});
		}
	});
}

function createPdfByStudentId(studentId) {
	$.messager.confirm('系统提示', '是否提交生成成长足迹', function(r) {
		if (r){
			$.ajax({
				url : createPdfByStudentIdUrl,
				type : 'POST',
				error : function() {
					$.messager.progress('close');
					$.messager.alert('系统提示', '操作异常', 'error');
				},
				data : {
					studentId: studentId
				},
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						reload();
						$.messager.alert('系统提示', '成长足迹创建成功', 'info');
					} else {
						$.messager.alert('系统提示', '成长足迹创建失败', 'warning');
					}
				}
			});
		}
	});
}
function getFileList(studentId) {
	$.ajax({
		url : getFileListUrl,
		type : 'POST',
		error : function() {
			$.messager.progress('close');
			$.messager.alert('系统提示', '操作异常', 'error');
		},
		data : {
			studentId: studentId
		},
		success : function(data) {
			if(data != null) {
				var html = '', firstHtml = '';
				for(var i in data) {
					if('成长足迹.pdf' == data[i]) {
						firstHtml = "<div class='col-md-12 p10'>" +data[i]+ "<a class='btn btn-success btn-xs' style='margin-left: 40px;' target='_blank' href='"+imgUrl + i +"'>查看<a></div>";
					}else {
						html += "<div class='col-md-12 p10'>" +data[i]+ "<a class='btn btn-success btn-xs' style='margin-left: 40px;' target='_blank' href='"+imgUrl + i +"'>查看<a></div>";
					}
				}
				$("#fileList").html(firstHtml + html);
				$('#confirmDialog').dialog({
					title : "我的成长足迹",
					modal : true,
					width : 300,
					height: 250,
					top: 50,
					draggable : true,
					resizable : true,
					onClose : function() {
						$("#fileList").html("");
					}
				}).show();
			}
		}
	});
}
